package club.mrxiao.gps.socket.util;

import java.math.BigInteger;

/**
 * Int数组与16进制字符串相互转换的工具类
 * 
 * @author zhu@goome
 *
 */
public class Int2Hex {

	public final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
	 * 将10进制数字转化为16进制字符串
	 *
	 * @param num int数字
	 * @return num 数字的十六进制字符串表示
	 */
	public static String intToHex(int num) {
		if (num < 0) {
			num = num + 256;
		}
		int d1 = num / 16;
		int d2 = num % 16;
		String s = hexArray[d1] + hexArray[d2];
		return s.toUpperCase();
	}

	/**
	 * 将10进制数字数组转化为16进制字符串
	 *
	 * @param ints int[] 数组
	 * @return int 数组的十六进制字符串表示
	 */
	public static String intsToHex(int[] ints) {
		StringBuffer s = new StringBuffer();
		for (int n : ints) {
			String s1 = intToHex(n);
			s.append(s1);
		}
		return s.toString();
	}

	/**
	 * 將16進制字符串轉換為10進制數字
	 * @param hex
	 * @return
	 */
	public static int decodeHex(String hex) {
		BigInteger bigint = new BigInteger(hex, 16);
		return bigint.intValue();
	}
}
